package com.changgou.order.controller;

import com.changgou.order.dao.OrderMapper;
import com.changgou.order.pojo.Order;
import com.changgou.order.pojo.Remind;
import com.changgou.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import entity.DateUtil;
import entity.Result;
import entity.StatusCode;

import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/****
 * @Author:keda
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private TokenDecode tokenDecode;
    @Autowired
    private OrderMapper orderMapper;


    /**
     * 根据用户名分页排序条件查询订单
     * @param order
     * @param page
     * @return
     */
    @RequestMapping("/search/{page}")
    public Result findAllOrder(@RequestBody(required = false)Order order,@PathVariable("page") int page){
        try {
            Map<String,Object> dataMap= orderService.findAllOrder(order,page);
            return new Result<>(true,StatusCode.OK,"查询订单成功",dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询订单失败");
        }
    }

    /**
     * 查询所有状态订单个数
     * @return
     */
    @RequestMapping("/findCount")
    public Result findCount(){
        try {
            Map<String,Object> dataMap= orderService.findCount();
            return new Result(true,StatusCode.OK,"查询个数成功",dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询个数失败");
        }
    }

    /**
     * 删除订单(修改状态)
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/deleteOrder/{orderId}")
    public Result deleteOrder(@PathVariable String orderId) {
        orderService.deleteOrder(orderId);
        return new Result(true, StatusCode.OK, "删除成功");
    }


    /**
     * 确认收货(修改状态)
     *
     * @param id
     * @return
     */
    @RequestMapping("/confirmOrder/{id}")
    public Result confirmReceipt(@PathVariable String id) {
        orderService.confirmReceipt(id);
        return new Result(true, StatusCode.OK, "确认收货成功");
    }

    /**
     * 提醒发货
     *
     * @param orderId : 订单id
     * @return
     */
    @RequestMapping("/remind/{orderId}")
    public Result remindTheShipment(@PathVariable String orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        Date payTime = order.getPayTime();

        String msg = null;
        if (System.currentTimeMillis() < DateUtil.addDateHour(payTime, 24).getTime()) {
            msg = "亲! 订单在正常时效内哟!";
        } else {
            //先查询数据库
            Remind remind = orderService.remindTheShipment(orderId);

            if (remind == null) {
                //增加数据库
                orderService.addRemind(orderId);
                msg = "亲! 提醒商家了,商家会马上联系您";
            } else {
                if (remind.getStatus().equalsIgnoreCase("1")) {
                    msg = "亲! 一天之内不能多次提醒哦";
                }
                if (remind.getStatus().equalsIgnoreCase("0")) {
                    orderService.updateRemindStatus(remind.getId());
                    msg = "亲! 提醒商家了,商家会马上联系您";
                }
            }
        }
        return new Result(true, StatusCode.OK, msg);


    }


    /**
     * 根据id取消(修改状态)订单
     *
     * @param id b v6b5tg
     * @return
     */
    @RequestMapping("/updateOrder/{id}")
    public Result CancellationOfOrder(@PathVariable String id) {
        try {
            orderService.CancellationOfOrder(id);
            return new Result(true, StatusCode.OK, "取消订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "该订单不能取消");
        }
    }

    /***
     * Order分页条件搜索实现
     * @param order
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Order order, @PathVariable int page, @PathVariable int size) {
        //调用OrderService实现分页条件查询Order
        PageInfo<Order> pageInfo = orderService.findPage(order, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * Order分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用OrderService实现分页查询Order
        PageInfo<Order> pageInfo = orderService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param order
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Order>> findList(@RequestBody(required = false) Order order) {
        //调用OrderService实现条件查询Order
        List<Order> list = orderService.findList(order);
        return new Result<List<Order>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        //调用OrderService实现根据主键删除
        orderService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改Order数据
     * @param order
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Order order, @PathVariable String id) {
        //设置主键值
        order.setId(id);
        //调用OrderService实现修改Order
        orderService.update(order);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 下单，并执行下单相关的一系列操作
     * @param order
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Order order) {
        //获取用户名
        String username = tokenDecode.getUserInfo().get("username");
        //调用OrderService实现添加Order
        order.setUsername(username);
        orderService.add(order);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询Order数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Order> findById(@PathVariable String id) {
        //调用OrderService实现根据主键查询Order
        Order order = orderService.findById(id);
        return new Result<Order>(true, StatusCode.OK, "查询成功", order);
    }

    /***
     * 查询Order全部数据
     * @return
     */
    @GetMapping
    public Result<List<Order>> findAll() {
        //调用OrderService实现查询所有Order
        List<Order> list = orderService.findAll();
        return new Result<List<Order>>(true, StatusCode.OK, "查询成功", list);
    }
}
